package net.osweb.core.tools.utils.ftp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import net.osweb.core.tools.exception.FileUploadException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.net.ftp.FtpClient;

public class FtpFileDownload {

	final static Log log = LogFactory.getLog(FtpFileDownload.class);
	
	public static boolean ftpFileDownload(String ip,
			String ftpUser, String ftpPassword, String fileName, String ftpPath,HttpServletResponse response) throws FileUploadException {
		FtpClient ftpClient = null;
		OutputStream outputStream = null;
		InputStream inputStream = null;
		boolean flag = true;
		try {
			response.reset();//设置为没有缓�?
			response.setContentType("application/x-download;charset=GBK");
			response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"), "8859_1"));
			ftpClient = new FtpClient();
			ftpClient.openServer(ip);
			ftpClient.login(ftpUser, ftpPassword);
			// 进入服务器的指定目录
			ftpClient.cd(ftpPath);
			ftpClient.binary();			
			outputStream = response.getOutputStream();
			inputStream = ftpClient.get(fileName);
			byte[] bytes = new byte[1024];
			int readBye;
			while ((readBye = inputStream.read(bytes)) != -1) {
				outputStream.write(bytes, 0, readBye);
			}
			outputStream.flush();
		} catch (Exception e) {
			log.error(e);
			flag = false;
			throw new FileUploadException(e);
		}finally{
				try {
					if(inputStream != null)
					inputStream.close();
					if(outputStream != null)
					outputStream.close();
					ftpClient.closeServer();
				} catch (IOException e) {
					log.error(e);
					flag = false;
					throw new FileUploadException(e);
				}
			
		}
		return flag;
	}
	
	public static boolean ftpFileDownload(String fileName ,HttpServletResponse response) throws FileUploadException {
		FtpClient ftpClient = null;
		OutputStream outputStream = null;
		InputStream inputStream = null;
		boolean flag = true;
		try {
			String ip = FileConfig.getInstance().getIp();
			String ftpPath = FileConfig.getInstance().getFtpPath();
			@SuppressWarnings("unused")
			String name = FileConfig.getInstance().getName();
			@SuppressWarnings("unused")
			String ftpPort = FileConfig.getInstance().getFtpPort();
			@SuppressWarnings("unused")
			String httpPort = FileConfig.getInstance().getHttpPort();
			@SuppressWarnings("unused")
			String httpPath = FileConfig.getInstance().getHttpPath();
			String ftpUser = FileConfig.getInstance().getFtpUser();
			String ftpPassword = FileConfig.getInstance().getFtpPassword();
			response.reset();//设置为没有缓�?
			response.setContentType("application/x-download;charset=GBK");
			response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"), "8859_1"));
			ftpClient = new FtpClient();
			ftpClient.openServer(ip);
			ftpClient.login(ftpUser, ftpPassword);
			// 进入服务器的指定目录
			ftpClient.cd(ftpPath);
			ftpClient.binary();			
			outputStream = response.getOutputStream();
			inputStream = ftpClient.get(fileName);
			byte[] bytes = new byte[1024];
			int readBye;
			while ((readBye = inputStream.read(bytes)) != -1) {
				outputStream.write(bytes, 0, readBye);
			}
			outputStream.flush();
		} catch (Exception e) {
			log.error(e);
			flag = false;
			throw new FileUploadException(e);
		}finally{
				try {
					if(inputStream != null)
					inputStream.close();
					if(outputStream != null)
					outputStream.close();
					ftpClient.closeServer();
				} catch (IOException e) {
					log.error(e);
					flag = false;
					throw new FileUploadException(e);
				}
			
		}
		return flag;
	}

}
